package com.csust.automaticteach.utils.template.generate;

import com.csust.automaticteach.model.dto.generateexercises.BaseGenerateRequest;
import com.csust.automaticteach.model.entity.Exercises;
import com.csust.automaticteach.model.entity.User;
import com.csust.automaticteach.model.vo.exercises.ExercisesVO;
import com.csust.automaticteach.model.vo.exercises.GenExercisesVO;
import com.csust.automaticteach.service.ExercisesService;
import com.csust.automaticteach.service.ExercisesSetsService;
import com.csust.automaticteach.service.SetExercisesService;
import lombok.Getter;

import java.util.List;

/**
 * @ClassName AbstractGeneratorTemplate
 * @Description 生成类模板
 * @Author钰玟
 * @Date 2025/7/24 上午11:19
 * @Version 1.0
 **/

public abstract class AbstractGeneratorTemplate<REQUEST extends BaseGenerateRequest, RESPONSE> {
    // 服务依赖通过构造函数注入，保证不可变
    @Getter
    private final ExercisesService exercisesService;
    @Getter
    private final ExercisesSetsService exercisesSetsService;
    @Getter
    private final SetExercisesService setExercisesService;

    public AbstractGeneratorTemplate(ExercisesService exercisesService, ExercisesSetsService exercisesSetsService, SetExercisesService setExercisesService) {
        this.exercisesService = exercisesService;
        this.exercisesSetsService = exercisesSetsService;
        this.setExercisesService = setExercisesService;
    }

    // 动态参数，每次调用时设置
    protected REQUEST request;
    protected User loginUser;
    protected Long userId;
    protected String title;
    protected String chatId;
    /**
     * 对外暴露的生成方法，接收动态参数
     */
    public final GenExercisesVO generate(REQUEST request, User loginUser) {
        // 初始化动态参数
        this.request = request;
        this.loginUser = loginUser;
        // 执行模板流程
        return generateExercises();
    }

    private final GenExercisesVO generateExercises(){
        // 1. 初始化基础数据
        initBaseData();
        // 2. 准备生成参数
        prepareGenerateParams();
        // 3. 调用生成服务
        String result = callGenerateService();
        // 4. 解析生成结果（利用泛型避免类型转换）
        List<RESPONSE> responseList = parseResult(result);
        // 5. 转换为练习题列表
        List<Exercises> exercisesList = convertToExercises(responseList);
        // 6. 保存数据
        Long setId = saveData(exercisesList);
        GenExercisesVO genExercisesVO = this.convertGenExercisesVO(exercisesList, setId);
        return genExercisesVO;
    }

    /**
     * 初始化基础数据
     */
    protected void initBaseData() {
        this.userId = loginUser.getId();
        this.chatId = generateChatId();
        // 利用基类属性，统一处理userId
        request.setUserId(userId);
    }

    // 生成聊天ID
    protected String generateChatId() {
        return userId + String.valueOf(System.currentTimeMillis());
    }
    // 抽象方法：准备生成参数
    protected abstract void prepareGenerateParams();
    // 抽象方法：调用生成服务
    protected abstract String callGenerateService();
    // 抽象方法：解析结果（使用泛型返回具体响应类型）
    protected abstract List<RESPONSE> parseResult(String result);
    // 抽象方法：转换为练习题
    protected abstract List<Exercises> convertToExercises(List<RESPONSE> responseList);
    // 具体方法：保存数据
    protected abstract Long saveData(List<Exercises> exercisesList);
    private GenExercisesVO convertGenExercisesVO(List<Exercises> exercisesList, Long setId){
        List<ExercisesVO> exercisesVOList = exercisesList.stream().map(ExercisesVO::objToVO).toList();
        return new GenExercisesVO(setId, exercisesVOList);
    }
}
